import { subscribeWithSelector } from 'zustand/middleware'
import create from 'zustand'
import { useGlobalStore } from '@/store/global'
export type AiMtEntity = {
	/**会议纪要 */
	record: 0 | 1
	/**同声会议 */
	sign: 0 | 1
	/**会议签到 */
	subtitle: 0 | 1
}
type StateStore = {} & AiMtEntity

type IStore = StateStore & {
	update: (d: Partial<StateStore>) => void
	reset: () => void
	/**初始化数据*/
	init: (_data: null | AiMtEntity) => void
	/**获取页面参数*/
	getAiData: () => AiMtEntity | undefined
}

const initialState: StateStore = {
	record: 0, // 会议纪要
	sign: 0, // 会议签到
	subtitle: 0, // 同声会议
}
export const useMeetingAiStore = create<IStore>()(
	subscribeWithSelector((set, get) => ({
		...initialState,
		update: (d) => set({ ...d }),
		reset: () => set(initialState),
		/**初始化界面 */
		init: async (ai) => {
			if (ai) {
				set({
					record: ai.record,
					sign: ai.sign,
					subtitle: ai.subtitle,
				})
			} else {
				set({ record: 0, sign: 0, subtitle: 0 })
			}
		},
		/**获取AI数据 */
		getAiData: () => {
			const serverInfo = useGlobalStore.getState().serverInfo
			const user = useGlobalStore.getState().user
			const SWITCH = useGlobalStore.getState().SWITCH
			const { record, sign, subtitle } = get()
			if (!(serverInfo?.enable_kis && user?.enable_kis) || !SWITCH.kis.ai) {
				return undefined
			} else {
				return {
					record: record,
					sign: sign,
					subtitle: subtitle,
				}
			}
		},
	}))
)
